﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

using LowLevelGraphics;
using LowLevelGraphics.CustomControls;

namespace ImageRecognition2.Forms
{
    /// <summary>
    /// Size Rotation
    /// </summary>
    public partial class FormSizeRotation : Form, IFormFilter
    {
        protected Image m_ImageOriginal = null;
        protected Image m_ImageNew = null;
        /// <summary>
        /// Initializes a new instance of the <see cref="FormSizeRotation"/> class.
        /// </summary>
        public FormSizeRotation()
        {
            InitializeComponent();
            this.Shown += new EventHandler(FormSizeRotation_Shown);
            pbNew.BackgroundImageLayout = ImageLayout.Zoom;
            pbOriginal.BackgroundImageLayout = ImageLayout.Zoom;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void FormSizeRotation_Shown(object sender, EventArgs e)
        {
            m_ImageOriginal = (Bitmap)MainFormSingleton.ActivePictureBox.BackgroundImage.Clone();
            this.pbOriginal.BackgroundImage = m_ImageOriginal;
            this.pbNew.BackgroundImage = m_ImageOriginal;
        }

        /// <summary>
        /// Handles the Enter event of the groupBoxPrintingSize control.
        /// </summary>
        /// <param name="sender">The source of the e dvent.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void groupBoxPrintingSize_Enter(object sender, EventArgs e)
        {

        }

        private void rad45_CheckedChanged(object sender, EventArgs e)
        {
            m_ImageNew = BitmapFunctions.RotateImageByAngle((Bitmap)m_ImageOriginal.Clone(), 45.0d);
            this.pbNew.BackgroundImage = m_ImageNew;
            pbNew.Invalidate();
        }

        private void rad90_CheckedChanged(object sender, EventArgs e)
        {
            m_ImageNew = BitmapFunctions.RotateImageByAngle((Bitmap)m_ImageOriginal.Clone(), 90.0d);
            this.pbNew.BackgroundImage = m_ImageNew;
            pbNew.Invalidate();
        }

        private void rad180_CheckedChanged(object sender, EventArgs e)
        {
            m_ImageNew = BitmapFunctions.RotateImageByAngle((Bitmap)m_ImageOriginal.Clone(), 180.0d);
            this.pbNew.BackgroundImage = m_ImageNew;
            pbNew.Invalidate();
        }

        private void radArbitrary_CheckedChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbArbitrary.Text))
            {
                double dArbitrary = Double.Parse(tbArbitrary.Text, CultureInfo.InvariantCulture);
                m_ImageNew = BitmapFunctions.RotateImageByAngle((Bitmap)m_ImageOriginal.Clone(), dArbitrary);
                this.pbNew.BackgroundImage = m_ImageNew;
                pbNew.Invalidate();
            }
        }

        /// <summary>
        /// Gets or sets image original
        /// </summary>
        public Image imgOriginal
        {
            get { return m_ImageOriginal; }
            set { m_ImageOriginal = value; }
        }

        /// <summary>
        /// Return selectable picturebox
        /// </summary>
        public SelectablePictureBox PictureBox
        {
            get { return pbNew; }
        }

        /// <summary>
        /// OK
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbOK_Click(object sender, EventArgs e)
        {
            MainFormSingleton.ActivePictureBox.BackgroundImage = m_ImageNew;
            this.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
